package com.zmcn.msg.business.phoneGroup.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zmcn.msg.bean.po.Phone;
import com.zmcn.msg.bean.po.PhoneGroup;
import com.zmcn.msg.bean.po.PhoneToGroup;
import com.zmcn.msg.bean.po.User;
import com.zmcn.msg.bean.vo.PhoneVO;
import com.zmcn.msg.business.phoneGroup.vo.PhoneGroupVO;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.dao.BaseDao;

/**
 * 
 * 分群信息数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class PhoneGroupDao extends BaseDao {
	/**
	 * 获取分群信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<PhoneGroupVO> getList(Page<PhoneGroupVO> page) {
		String hqlResult = "select new " + PhoneGroupVO.class.getName() + PhoneGroupVO.STRUCT;
		String hqlCount = "select count(phonegroup)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(PhoneGroup.class.getName()).append(" phonegroup,");
		sb.append(User.class.getName()).append(" u");
		sb.append(" where phonegroup.userId=u.userId");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("groupName".equals(key)) {
				sb.append(" and phonegroup.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} else if("name".equals(key)){
				sb.append(" and u.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			}
		}
		
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 获取特定群手机信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<PhoneVO> getPhoneList(Page<PhoneVO> page) {
		String hqlResult = "select new " + PhoneVO.class.getName() + PhoneVO.STRUCT;
		String hqlCount = "select count(phone)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Phone.class.getName()).append(" phone,");
		sb.append(PhoneToGroup.class.getName()).append(" p");
		sb.append(" where phone.phoneId=p.phoneId");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("name".equals(key) || "phoneNo".equals(key)) {
				sb.append(" and phone.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} else if ("groupId".equals(key)) {
				sb.append(" and p.").append(key).append(" = ?");
				ps.add(paras.get(key)[0]);
			}
		}
		sb.append(" order by phone.phoneNo");
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

}
